home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2776 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.0 KB

  1. Path: news.compuserve.com!newsmaster
  2. From: 76623,2065@compuserve.com  (Bobby Martin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Growing Objects
  5. Date: 19 Jan 1996 16:55:13 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4doidh$16g@dub-news-svc-2.compuserve.com>
  8. References: <30FE8297.41C6@bme.jhu.edu>
  9. Reply-To: 76623,2065@compuserve.com (Bobby Martin)
  10. NNTP-Posting-Host: ad35-133.compuserve.com
  11. X-Newsreader: IBM NewsReader/2 v1.03
  12.  
  13. In <30FE8297.41C6@bme.jhu.edu>, Harold Bien <hbien@bme.jhu.edu> writes:
  14. >First - thanks to all who responded to my earlier question.  I
  15. >found all the comments helpful and was (ashamedly) surprised at the
  16. >number of responses.
  17. >
  18. >     That said, I've decided to implement a "possible" solution which
  19. >has not yet been discussed.  Before I begin, let me preface this with
  20. >the fact that I've invested quite a lot of time and effort into the 
  21. >simulation already and thus am not very willing to switching languages.  
  22. >Therefore, my current implementation is in C++ - my original language of 
  23. >choice.  However, I did look into SmallTalk as per one suggestion and 
  24. >found that SmallTalk would have been better if it were not for the fact 
  25. >that it's an interpreted language (or at least the version we have).  
  26. >Anyway - this is all off topic.....
  27.  
  28. bunch of stuff deleted...
  29.  
  30. >class Cell
  31. >{
  32. > public: // These fuctions are accessible to the public
  33. >      Cell(); /* This is the constructor - it gets called first when the 
  34. >                 object is created */
  35. > // Run is the function which gets called from the 'outside'
  36. > void Run();
  37. > // PreMature is the function which runs when Run() is called and age<4
  38. > void Run_PreMature();
  39. > // PostMature is the function which is called when Run() is invoked 
  40. > //   and age >=4
  41. > void Run_PostMature();
  42. >
  43. > private:
  44. >        void    (*Cell::RunFunc)(); /* Pointer to a cell function */
  45. >        unsigned int    age;
  46. >};
  47. >        
  48. >        Cell::Cell() /* Constructor */
  49. >{
  50. > age=0; // Newborn
  51. > RunFunc=Run_PreMature; /* Point RunFunc to PreMature() function */
  52. >}
  53. >
  54. >void    Cell:Run_PreMature()
  55. >{
  56. > age=age+1; /* I know: (age++), but for those who don't know C... */
  57. > if (age>=4) RunFunc=Run_PostMature; /* If age greater than or equal to
  58. >4
  59. >                                        then point RunFunc to
  60. >PostMature()
  61. >                                     */
  62. >}
  63.  
  64. a bunch more stuff deleted :)
  65.  
  66.     Try this: create base class CellAge, with a pure virtual function Run
  67. and have child classes CellPreMature and CellPostMature.  Then give Cell
  68. member data cellAge of the type CellWithAge* instead of the RunFunc.  Then
  69. you can manipulate the cellAge pointer to point to an instance of CellPreMature
  70. or CellPostMature as appropriate, and call cellAge->Run() instead of RunFunc.
  71. This eliminates the need for a pointer to a function, and may offer some
  72. added flexibility in the future as you decide there is really another age group,
  73. CellDecrepit.
  74.  
  75.     You may find that some of your member data or methods migrate upwards
  76. to CellAge.
  77.  
  78. Hope that helps.
  79.  
  80.